﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

using NuMetaheuristics.Genotypes;
using NuMetaheuristics.Operators.Vector;
using NuMetaheuristics.Operators.Value;

namespace NuMetaheuristics.Contexts.Vector.Boolean
{
    [DataContract(Namespace = "http://numetaheuristics.codeplex.com/schemas")]
    abstract public class BooleanVectorContext : IContext<BooleanVectorGenotype>
    {
        [DataMember]
        private int _length;

        [DataMember]
        private IUnaryOperator<BooleanVectorGenotype> _initializer;

        public BooleanVectorContext()
        {
        }

        public BooleanVectorContext(int length)
        {
            _length = length;
            _initializer = new BooleanVectorInitializer(new BooleanRandomInitializer());
        }

        public BooleanVectorGenotype Produce(bool initialize, bool makeValid, bool evaluate)
        {
            var candidate = new BooleanVectorGenotype(_length);

            if (initialize)
            {
                Initialize(ref candidate);
            }

            if (makeValid && !IsValid(candidate))
            {
                MakeValid(ref candidate);
            }

            if (evaluate)
            {
                Evaluate(ref candidate);
            }

            return candidate;
        }

        public void Initialize(ref BooleanVectorGenotype candidate)
        {
            _initializer.Operate(ref candidate);
        }

        abstract public void Evaluate(ref BooleanVectorGenotype genome);

        public bool IsValid(BooleanVectorGenotype candidate)
        {
            return true;
        }

        public void MakeValid(ref BooleanVectorGenotype candidate)
        {
        }
    }
}
